<%--                                                                       --%>
<%-- Copyright (c) 2005 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ page import =  "java.util.*, java.net.*,
                    com.broadvision.web.util.ViewDynaBean,
                    org.apache.struts.validator.DynaValidatorForm,
                    org.apache.commons.beanutils.PropertyUtils"
%>

<%@ taglib uri="/WEB-INF/bv-html.tld" prefix="bvhtml" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<script language="javascript" src="<%=request.getContextPath()%>/mt/js/userPopup.js"></script>

<SCRIPT language="javascript">

  function gotoFirstPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      thisForm.currentPageNumber.value = 1;
      thisForm.submit();
    }
  }
  
  function gotoPreviousPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber--;
      if (thisForm.entriesPerPage.value != <bean:write name="cmCategoryRoleUserListForm" property="entriesPerPage"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber > 0) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }
  
  function gotoNextPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber++;
      if (thisForm.entriesPerPage.value != <bean:write name="cmCategoryRoleUserListForm" property="entriesPerPage"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber <= thisForm.totalPageNumber.value) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }
  
  function gotoLastPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage.value != <bean:write name="cmCategoryRoleUserListForm" property="entriesPerPage"/>) {
        thisForm.currentPageNumber.value = 1;
      }
      else {
        thisForm.currentPageNumber.value = thisForm.totalPageNumber.value;
      }
      thisForm.submit();
    }
  }

  function doGo(formName) {
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage.value != <bean:write name="cmCategoryRoleUserListForm" property="entriesPerPage"/>) {
        thisForm.currentPageNumber.value = 1;
        thisForm.submit();
        return true;
      }
    }
    return false;
  }
  
  function checkNumberOnly(e) {
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    }
    else {
      return true;
    }
  }

  function doValidateEntriesPerPage(formName) {
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage == null) {
        return true;
      }
      var entriesPerPageString = thisForm.entriesPerPage.value;
      var len = entriesPerPageString.length;
      var i = 0;
      for (i = 0; i < len; i++) {
        if (entriesPerPageString.charAt(i) < '0' || entriesPerPageString.charAt(i) > '9') {
          alert("<bean:message bundle='cm.messages' key="cm.common.pagination.validation.itemsPerPage"/>");
          thisForm.entriesPerPage.select();
          thisForm.entriesPerPage.focus();
          return false;
        }
      }
      if (thisForm.entriesPerPage.value < 1 || thisForm.entriesPerPage.value > 2147483647) {
        alert("<bean:message bundle='cm.messages' key="cm.common.pagination.validation.itemsPerPage"/>");
        thisForm.entriesPerPage.select();
        thisForm.entriesPerPage.focus();
        return false;
      }
      else {
        return true;
      }
    }
    return false;
  }

  //
  // 'callback' function to the parentWindow to set the user aliases (for multiple selections)
  //

  function setSelectedUsers(formName, fieldName, users) {
    var thisForm = document.forms[formName];
    var elements = thisForm.elements;
    var numElements = elements.length;
    var field = null;
    for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
      var elementName = elements[elementIndex].name;
      if (elementName == fieldName) {
        field = elements[elementIndex];
        break;
      }
    }
    if (field != null) {

      var numOriginalUsers = field.length;
      if (users != null) {
        var numUsers = users.length;
        var i = 0;
        for (i = 0; i < numUsers; i++) {
          if (i >= numOriginalUsers) {
            var newoption = new Option(users[i], users[i], true, true);
            field.options[i] = newoption;
          } else {
            field.options[i].text = users[i];
            field.options[i].value = users[i];
            field.options[i].defaultSelected = true;
            field.options[i].selected = true;
          }
        }
        for (var j = numOriginalUsers - 1; j >= i; j--) {
          field.options[j] = null;
        }
      } else {
        for (var i = numOriginalUsers - 1; i >= 0; i--) {
          field.options[i] = null;
        }
      }
    }

    thisForm.currentPageNumber.value = 0;
    thisForm.action='<html-el:rewrite page="/cm/cmCategoryRoleUserAdd.do"/>';
    thisForm.submit();
  }

  // popup new window to find and select users/groups
  function searchGroupPopup(popupUrl, parentFormName, parentFieldName) {
    var DELIM = '%24%23@!';
    var timestamp = getTimeStamp();
    if (popupUrl != null) {
      if (popupUrl.indexOf("?") < 0) {
        popupUrl += "?";
      }
      else {
        popupUrl += "&";
      }
    }
    popupUrl += 'timestamp=' + timestamp + '&parentFormName=' + parentFormName +
      '&parentFieldName=' + parentFieldName;
    var thisForm = document.forms[parentFormName];
    var elements = thisForm.elements;
    var numElements = elements.length;
    var field = null;
    for (var elementIndex = 0; elementIndex < numElements; elementIndex++) {
      var elementName = elements[elementIndex].name;
      if (elementName == parentFieldName) {
        field = elements[elementIndex];
        break;
      }
    }
    if (field != null && field.length > 0) {
      var selectedgroups = '&existingGroupFQNs=';
      for (var i = 0; i < field.length; i++) {
        selectedgroups += field.options[i].value;
          selectedgroups += DELIM;
      }
      popupUrl += selectedgroups;
    }
    popupUrl += '&callback=setNewGroups';
    popupUrl += '&minSelection=0&multiSelect=true&recursiveCheck=true';

    var windowName = parentFormName + parentFieldName;
    var windowFeatures = 'width=700,height=600,scrollbars=yes,resizable=yes';
    var popupWindow = window.open(popupUrl, windowName, windowFeatures);
    if (popupWindow != null) {
      if (popupWindow.opener == null) {
        popupWindow.opener = self;
      }
    }
    popupWindow.focus();
  }

  //
  // Call back function
  //
  function setNewGroups(ids, names) {
    document.cmCategoryRoleUserListForm.newGroups.length = 0;
    for (var index = 0; index < ids.length; index++) {
      var option = new Option(" ");
      option.text = names[index];
      option.value = ids[index];
      option.defaultSelected = true;
      option.selected = true;
      document.cmCategoryRoleUserListForm.newGroups.options[index] = option;
    }
    document.cmCategoryRoleUserListForm.currentPageNumber.value = 0;
    document.cmCategoryRoleUserListForm.action='<html-el:rewrite page="/cm/cmCategoryRoleGroupAdd.do"/>';
    document.cmCategoryRoleUserListForm.submit();
  }

</SCRIPT>
<%
  java.util.HashMap map = new java.util.HashMap();
  DynaValidatorForm cmCategoryRoleUserListForm = (DynaValidatorForm) request.getAttribute("cmCategoryRoleUserListForm");
  map.put("oid", (Long) PropertyUtils.getSimpleProperty(cmCategoryRoleUserListForm, "oid"));
  pageContext.setAttribute("pagetabParam", map, PageContext.PAGE_SCOPE);
%>

  
<html:form action="/cm/cmCategoryRoleUserList.do" onsubmit="return doGo('cmCategoryRoleUserListForm');">
<html:hidden name="cmCategoryRoleUserListForm" property="oid"/>
<html:hidden name="cmCategoryRoleUserListForm" property="groupName"/>
<html:hidden name="cmCategoryRoleUserListForm" property="currentPageNumber"/>
<html:hidden name="cmCategoryRoleUserListForm" property="totalPageNumber"/>
<html:hidden name="cmCategoryRoleUserListForm" property="lastPage"/>
<html:hidden name="cmCategoryRoleUserListForm" property="userAlias"/>
<html:hidden name="cmCategoryRoleUserListForm" property="groupFQN"/>
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr> 
    <td valign="top" width="100%">
      <div class="pageTitle">
         <bean:write name="cmCategoryRoleUserListView" property="roleName"/>
      </div>
      <div class="errorMessage"><html:errors bundle='cm.messages' /></div>
      <%
        ViewDynaBean roleUserListView = (ViewDynaBean) request.getAttribute("cmCategoryRoleUserListView");
        Integer userLength = (Integer) PropertyUtils.getSimpleProperty(roleUserListView, "numOfUser");
      %>
      <%@include file="/mc/common/pagetab.jsp" %>

      <div class="pageSubtitle">
        <span class="right">
             <input type="button" name="browseUsers" class="pageButton" 
                    onClick="JavaScript:userMultiSearchPopup('<html-el:rewrite page="/mt/userMultiSearchPopup.do"/>', 'cmCategoryRoleUserListForm', 'newUsers')"
                    value='<bean:message bundle='cm.messages' key="cm.content.roleUser.selectUserButton"/>'>

             <select name="newUsers" multiple="multiple" size="0" style="visibility: hidden; display: none;">
               <c:forEach var="oneUser" items="${cmCategoryRoleUserListForm.map.originalUserList}" >
                 <option selected value="<c:out value="${oneUser}" />"><c:out value="${oneUser}" /></option>
               </c:forEach>
             </select>
             <select name="originalUsers" multiple="multiple" size="0" style="visibility: hidden; display: none;">
               <c:forEach var="oneUser" items="${cmCategoryRoleUserListForm.map.originalUserList}" >
                 <option selected value="<c:out value="${oneUser}" />"><c:out value="${oneUser}" /></option>
               </c:forEach>
             </select>

             <input type="button" name="browseGroup" class="pageButton"
                    onClick="JavaScript:searchGroupPopup('<html-el:rewrite page="/mt/groupMultiPopup.do"/>', 'cmCategoryRoleUserListForm', 'newGroups');"
                    value='<bean:message bundle='cm.messages' key="cm.content.roleUser.selectGroupButton"/>'>

             <select name="newGroups" multiple="multiple" size="0" style="visibility: hidden; display: none;">
               <c:forEach var="oneGroup" items="${cmCategoryRoleUserListForm.map.originalGroupList}" >
                 <option selected value="<c:out value="${oneGroup}" />"><c:out value="${oneGroup}" /></option>
               </c:forEach>
             </select>
             <select name="originalGroups" multiple="multiple" size="0" style="visibility: hidden; display: none;">
               <c:forEach var="oneGroup" items="${cmCategoryRoleUserListForm.map.originalGroupList}" >
                 <option selected value="<c:out value="${oneGroup}" />"><c:out value="${oneGroup}" /></option>
               </c:forEach>
             </select>

        </span>

        <span class="left">
            <!--bean:write name="cmCategoryRoleUserListView" property="roleName" /-->
                <bean:define id="roleName" name="cmCategoryRoleUserListView" property="roleName"/>

            <bean:message bundle='cm.messages' key="cm.content.roleUser.result" arg0="<%= userLength.toString() %>" />
        </span>
        <span class="right">&nbsp;</span>

        </span>
      </div> 
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Content Services role member list" width="100%">
        <tr class="listHeader">
          <th valign="top" id="alias" align="left" width="30%" >
            <bean:message bundle='cm.messages' key="cm.content.roleUser.aliasNGroup"/>
          </th>
          <th valign="top" id="formatName" align="left" width=50%" >
            <bean:message bundle='cm.messages' key="cm.content.roleUser.formatNameNGroupFQN"/>
          </th>
          <th valign="top" id="actions" align="left" nowrap width="20%" >
            <bean:message bundle='cm.messages' key="header.actions"/>
          </th>
        </tr>
        <logic:empty name="cmCategoryRoleUserListView" property="instances">
          <tr class="listEven">
            <td align="center" colspan="5">
              <bean:message bundle='cm.messages' key="cm.content.roleUser.noRoleUserList"/>
            </td>
          </tr>
        </logic:empty>
        <logic:notEmpty name="cmCategoryRoleUserListView" property="instances">
          <logic:iterate id="instance" name="cmCategoryRoleUserListView" property="instances" indexId="i">
            <tr class="<%=i.intValue() % 2 == 0 ? "listEven" : "listOdd" %>">
              <logic:equal name="instance" property="memberType" value="0">
                <td valign="top"  headers="alias">
                  <img src="<%=request.getContextPath()%>/bp/images/icons/individual.gif" border="0" alt="<bean:message bundle='cm.messages' key="image.alt.individual"/>" /> &nbsp;
                    <bean:write name="instance" property="alias"/>
                </td>
                <td valign="top" headers="formatName">
                    <bean:write name="instance" property="formattedName"/> 
                </td>
                <td valign="top" nowrap align="left" headers="actions">
                  <a href='<%=response.encodeURL(request.getContextPath() + "/cm/cmCategoryRoleUserRemove.do?userId=")%><bean:write name="instance" property="userId"/>&oid=<bean:write name="cmCategoryRoleUserListForm" property="oid"/>' >
                  <img src="<%=request.getContextPath()%>/mc/images/action/icon_remove.gif" width="15" height="15" border="0"  class="actionIcon" alt="<bean:message bundle='cm.messages' key="image.alt.remove"/>" /></a>
                </td>
              </logic:equal>

              <logic:equal name="instance" property="memberType" value="1">
                <td valign="top"  headers="alias">
                  <img src="<%=request.getContextPath()%>/bp/images/icons/group.gif" border="0" alt="<bean:message bundle='cm.messages' key="image.alt.group"/>" /> &nbsp;
                    <bean:write name="instance" property="groupName"/>
                </td>
                <td valign="top" headers="formatName">
                    <bean:write name="instance" property="groupFQN"/> 
                </td>
                <td valign="top" nowrap align="left" headers="actions">
                  <a href='<%=response.encodeURL(request.getContextPath() + "/cm/cmCategoryRoleGroupRemove.do?tempGroupFQN=")%><bean:write name="instance" property="groupFQN"/>&oid=<bean:write name="cmCategoryRoleUserListForm" property="oid"/>' >
                  <img src="<%=request.getContextPath()%>/mc/images/action/icon_remove.gif" width="15" height="15" border="0"  class="actionIcon" alt="<bean:message bundle='cm.messages' key="image.alt.remove"/>" /></a>
                </td>
              </logic:equal>

            </tr>
          </logic:iterate>
        </logic:notEmpty>
      </table>
      <logic:notEmpty name="cmCategoryRoleUserListView" property="instances">
        <div class="pagination">
          <bean:message bundle='cm.messages' key="cm.common.pagination.show"/>

          <bean-el:message key="cm.common.pagination.show" bundle="cm.messages"/>
          <html-el:select property="entriesPerPage" size="1" onchange="doGo('cmCategoryRoleUserListForm')">
            <html-el:option value="10">10</html-el:option>
            <html-el:option value="20">20</html-el:option>
            <html-el:option value="30">30</html-el:option>
            <html-el:option value="50">50</html-el:option>
            <html-el:option value="100">100</html-el:option>
          </html-el:select>

          <bean:message bundle='cm.messages' key="cm.common.pagination.itemsPerPage"/>
          &nbsp;
          <logic:greaterThan name="cmCategoryRoleUserListForm" property="totalPageNumber" value="1">
            &nbsp;&nbsp;&nbsp;
            <logic:greaterThan name="cmCategoryRoleUserListForm" property="currentPageNumber" value="0">
              <logic:greaterThan name="cmCategoryRoleUserListForm" property="totalPageNumber" value="1">
                <logic:greaterThan name="cmCategoryRoleUserListForm" property="currentPageNumber" value="1">
                  <a href="#" class="paginationBtn" onclick="javascript:gotoFirstPage('cmCategoryRoleUserListForm')">
                    <bean:message bundle='cm.messages' key="cm.common.pagination.first"/></a>
                  <a href="#" class="paginationBtn" onclick="javascript:gotoPreviousPage('cmCategoryRoleUserListForm')">
                    <bean:message bundle='cm.messages' key="cm.common.pagination.previous"/></a>
                </logic:greaterThan>
              </logic:greaterThan>
              <bean:define id="currentPageNumberAttr" name="cmCategoryRoleUserListForm" property="currentPageNumber"/>
              <bean:define id="totalPageNumberAttr" name="cmCategoryRoleUserListForm" property="totalPageNumber"/>
              <% Integer currentPageNumberInt = (Integer)pageContext.getAttribute("currentPageNumberAttr"); %>
              <% Integer totalPageNumberInt = (Integer)pageContext.getAttribute("totalPageNumberAttr"); %>
              <bean:message bundle='cm.messages' key="cm.common.pagination.pageOf" 
                arg0="<%= currentPageNumberInt.toString() %>" 
                arg1="<%= totalPageNumberInt.toString() %>"/>
              <logic:greaterThan name="cmCategoryRoleUserListForm" property="totalPageNumber" value="1">
                <logic:equal name="cmCategoryRoleUserListForm" property="lastPage" value="false">
                  <a href="#" class="paginationBtn" onclick="javascript:gotoNextPage('cmCategoryRoleUserListForm')">
                    <bean:message bundle='cm.messages' key="cm.common.pagination.next"/></a>
                  <a href="#" class="paginationBtn" onclick="javascript:gotoLastPage('cmCategoryRoleUserListForm')">
                    <bean:message bundle='cm.messages' key="cm.common.pagination.last"/></a>
                </logic:equal>
              </logic:greaterThan>
            </logic:greaterThan>
          </logic:greaterThan>
        </div>
      </logic:notEmpty>
      <br>
    </td>
  </tr>
</table>
</html:form>
